Public Class frmMain

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        dlgOpen.RootFolder = Environment.SpecialFolder.ProgramFiles
        dlgOpen.ShowDialog()
        If MsgBox("Create new subdirectory '" & dlgOpen.SelectedPath & "\ERN'?", MsgBoxStyle.YesNo + MsgBoxStyle.DefaultButton1) = MsgBoxResult.Yes Then
            txtLocation.Text = dlgOpen.SelectedPath & "\ERN"
        Else
            txtLocation.Text = dlgOpen.SelectedPath
        End If
    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Application.Exit()
    End Sub

    Private Sub btnInstall_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInstall.Click
        doInstall()
        MsgBox("Installation complete!")
        Application.Exit()
    End Sub

    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim i As Integer

        Me.Visible = False
        txtLocation.Text = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) & "\ERN"

        If System.Environment.GetCommandLineArgs().Length > 1 Then
            For i = 1 To Environment.GetCommandLineArgs().Length - 1
                If Split(System.Environment.GetCommandLineArgs()(i), "=")(0) = "-i" Then
                    Me.Visible = True
                ElseIf Split(System.Environment.GetCommandLineArgs()(i), "=")(0) = "-l" Then
                    txtLocation.Text = Split(System.Environment.GetCommandLineArgs()(i), "=")(1)
                End If
            Next
        End If

        If Not Me.Visible Then
            doInstall()
            Application.Exit()
        Else
            If IO.File.Exists(Application.StartupPath & "\license.txt") Then
                If frmLicense.ShowDialog() = Windows.Forms.DialogResult.Cancel Then
                    MsgBox("You must accept the license agreement if you wish to use this program.  Please rerun this setup program if you decide to accept the agreement.")
                    Application.Exit()
                End If
            End If
        End If
    End Sub

    Private Sub doInstall()
        Dim procInfo As New Diagnostics.ProcessStartInfo
        Dim uninstallFile As IO.TextWriter
        Dim unFiles() As String
        Dim unRegs() As String
        Dim unFile As Integer = 0
        Dim unReg As Integer = 0
        Dim i As Integer
        Dim fileCopyOK As Boolean = True

        Const fileCount As Integer = 5
        Const regCount As Integer = 1
        Const dirCount As Integer = 1

        ReDim unFiles(fileCount)
        ReDim unRegs(regCount)

        btnExit.Enabled = False
        btnInstall.Enabled = False

        progress.Step = Math.Ceiling(100 / (dirCount + fileCount + regCount + 1 + 1))

        'create directory
        lblStatus.Text = "Creating directories..."
        IO.Directory.CreateDirectory(txtLocation.Text)
        progress.PerformStep()

        'copy files
        lblStatus.Text = "Copying files..."

        Try
            IO.File.Copy(Application.StartupPath & "\ziphelper.dll", txtLocation.Text & "\ziphelper.dll", True)
        Catch
            fileCopyOK = False
        End Try
        unFiles(unFile) = txtLocation.Text & "\ziphelper.dll"
        unFile = unFile + 1
        progress.PerformStep()
        Try
            IO.File.Copy(Application.StartupPath & "\ICSharpCode.SharpZipLib.dll", txtLocation.Text & "\ICSharpCode.SharpZipLib.dll", True)
        Catch
            fileCopyOK = False
        End Try
        unFiles(unFile) = txtLocation.Text & "\ICSharpCode.SharpZipLib.dll"
        unFile = unFile + 1
        progress.PerformStep()
        Try
            IO.File.Copy(Application.StartupPath & "\ERN_Client.exe", txtLocation.Text & "\ERN_Client.exe", True)
        Catch
            fileCopyOK = False
        End Try
        unFiles(unFile) = txtLocation.Text & "\ERN_Client.exe"
        unFile = unFile + 1
        progress.PerformStep()
        Try
            IO.File.Copy(Application.StartupPath & "\ErrorHandling.dll", txtLocation.Text & "\ErrorHandling.dll", True)
        Catch
            fileCopyOK = False
        End Try
        unFiles(unFile) = txtLocation.Text & "\ErrorHandling.dll"
        unFile = unFile + 1
        progress.PerformStep()
        Try
            IO.File.Copy(Application.StartupPath & "\euu.exe", txtLocation.Text & "\euu.exe", True)
        Catch
            fileCopyOK = False
        End Try
        progress.PerformStep()

        'modify registry
        lblStatus.Text = "Modifying the registry..."
        Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True).SetValue("ERN Client", txtLocation.Text & "\ern_client.exe")
        unRegs(unReg) = "HKLM::SOFTWARE\Microsoft\Windows\CurrentVersion\Run::ERN Client"
        unReg = unReg + 1
        progress.PerformStep()

        'build uninstall info file
        uninstallFile = IO.File.CreateText(txtLocation.Text & "\uninstallClient.log")
        uninstallFile.WriteLine("files:" & unFiles.Length - 2)
        uninstallFile.WriteLine("registries:" & unRegs.Length - 1)
        For i = 0 To unFiles.Length - 1
            If unFiles(i) <> "" Then uninstallFile.WriteLine(unFiles(i))
        Next
        For i = 0 To unRegs.Length - 1
            If unRegs(i) <> "" Then uninstallFile.WriteLine(unRegs(i))
        Next
        uninstallFile.Close()
        progress.PerformStep()

        'create the add/remove programs info
        Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", True).CreateSubKey("ERN Client")
        Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\ERN Client", True).SetValue("DisplayName", "ERN Client")
        Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\ERN Client", True).SetValue("DisplayVersion", "")
        Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\ERN Client", True).SetValue("HelpLink", "http://www.noworriescomputerhelp.com/ern")
        Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\ERN Client", True).SetValue("NoModify", "1", Microsoft.Win32.RegistryValueKind.DWord)
        Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\ERN Client", True).SetValue("NoRepair", "1", Microsoft.Win32.RegistryValueKind.DWord)
        Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\ERN Client", True).SetValue("Publisher", "Dan Cleveland")
        Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\ERN Client", True).SetValue("UninstallString", ControlChars.Quote & txtLocation.Text & "\euu.exe" & ControlChars.Quote & " uninstallClient.log")
        Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\ERN Client", True).SetValue("URLInfoAbout", "http://www.noworriescomputerhelp.com/ern")
        Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\ERN Client", True).SetValue("URLUpdateInfo", "http://www.noworriescomputerhelp.com/ern")
        Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\ERN Client", True).SetValue("DisplayIcon", ControlChars.Quote & txtLocation.Text & "\ern_client.exe" & ControlChars.Quote)
        progress.PerformStep()

        'execute ern client
        With procInfo
            .FileName = txtLocation.Text & "\ern_client.exe"
            .WorkingDirectory = txtLocation.Text
        End With
        Process.Start(procInfo)

        If Not fileCopyOK And Me.Visible Then MsgBox("Some destination files are in use and could not be copied", MsgBoxStyle.OkOnly, "ERN Client Setup")
    End Sub
End Class
